﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Chilkat;

namespace SeriousTalker
{
    public class Client : IDisposable
    {
        private Socket _socket;

        public Client()
        {
            _socket = new Socket();
            
            try
            {
                UnLock();
            }
            catch(Exception e)
            {
                MessageBox.Show("Error: " + e.Message);
            }
        }

        private void UnLock()
        {
            if (!_socket.UnlockComponent("Anything for 30-day trial"))
                throw new Exception("Failed to unlock component");
        }

        public void Connect(string address)
        {
            if(!_socket.Connect(address, 443, true, 20000))
                throw new Exception("Failed to connect to host at: " + address + "\n" + _socket.LastErrorText);
        }

        public void SendMessage(string message, string signature = "anonymous")
        {
            if(!_socket.SendString(message + ";" + signature))
                throw new Exception("Failed to send message: " + _socket.LastErrorText);
        }

        public void Dispose()
        {
            _socket.Close(20000);
        }
    }
}
